﻿using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigDrugsCtrlViewModel : BaseViewModel<ConfigDrugsCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";

        private ConfigDrugsModel _selectedP;
        private ConfigPrescriptionTypesModel _selectedPr;

        private ObservableCollection<ConfigDrugsModel> _displayConfigDrugsModel = new ObservableCollection<ConfigDrugsModel>();

        #endregion

        #region Constructor

        public ConfigDrugsCtrlViewModel()
        { }



        #endregion

        #region Properties

        public ObservableCollection<ConfigDrugsModel> DisplayConfigDrugsModel
        {
            get
            {
                _displayConfigDrugsModel.Clear();
                List<ConfigDrugsModel> lst = ConfigDrugsDataGridViewModel.Instance.ConfigDrugsModel.ToList().Where(O => O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigDrugsModel.Add(item);
                }
                return _displayConfigDrugsModel;
            }
            set
            {
                ConfigDrugsDataGridViewModel.Instance.ConfigDrugsModel = value;
                RaisePropertyChanged("DisplayConfigDrugsModel");
            }
        }

        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigDrugsModel");
            }
        }

        public ConfigDrugsModel SelectedP
        {
            get
            {
                return _selectedP;
            }

            set
            {
                if (value == null) return;
                _selectedP = value;
                RaisePropertyChanged("SelectedP");
            }
        }

        public ConfigPrescriptionTypesModel SelectedPr
        {
            get
            {
                return _selectedPr;
            }

            set
            {
                if (value == null) return;
                _selectedPr = value;
                RaisePropertyChanged("SelectedPr");
            }
        }


        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigDrugsModelService.Instance.Update(sender as ConfigDrugsModel);
        }
        private void AddConfigDrugsModel(ConfigDrugsModel obj)
        {
            List<ConfigDrugsModel> list = DataManager.Instance.ConfigDrugsModelList.ToList();
            MedicineConfigCtrlViewModel.Instance.InitialDrugList();
            SingleDrugConfigCtrlViewModel.Instance.InitialDrugList();
            AnesSystem.ViewModels.Operation.CQ.CQTL.MedicineConfigCtrlViewModel.Instance.InitialDrugList();
            ConfigDrugsModelService.Instance.Update(obj);
            //if (obj.Id != new ObjectId("000000000000000000000000"))
            //{

            //    ConfigDrugsModelService.Instance.Update(obj);
            //    ConfigDrugsDataGridViewModel.Instance.ConfigDrugsModel.Add(obj);
            //}
            //else ConfigDrugsModelService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods


        //测试存数据
        public void Update()
        {

            foreach (ConfigDrugsModel item in DataManager.Instance.ConfigDrugsModelList)
            {
                ConfigDrugsModelService.Instance.Update(item);
            }
        }
        public void DelConfigDrugsModel(ConfigDrugsModel configDrugsModel)
        {
            if (configDrugsModel != null)
            {
                ConfigDrugsDataGridViewModel.Instance.ConfigDrugsModel.Remove(configDrugsModel);
                RaisePropertyChanged("DisplayConfigDrugsModel");
                ConfigDrugsModelService.Instance.DeleteById(configDrugsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigDrugsModel model = new ConfigDrugsModel();
            model.Code = "drug_" + DateTime.Now.Ticks;
            model.Deleted = false;
            model.Name = "请编辑药名";
            model.Dose = 0;
            model.UnitName = "unit_2";
            model.DrugGroupTypeCode = "drugGroupType_6";
            model.DrugTypeCode = "drugType_1";
            model.PrescriptionName = "普通";
            model.PrescriptionTypeCode = "普通";
            model.Usage = "drug_7";
            DisplayConfigDrugsModel.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
            ConfigDrugsModelService.Instance.AddUpdate(model);
            DataManager.Instance.ConfigDrugsModelList.Add(model);
            MedicineConfigCtrlViewModel.Instance.InitialDrugList();
            SingleDrugConfigCtrlViewModel.Instance.InitialDrugList();
            AnesSystem.ViewModels.Operation.CQ.CQTL.MedicineConfigCtrlViewModel.Instance.InitialDrugList();
            
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigDrugsModel(sender as ConfigDrugsModel);
        }

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion

    }
}
